package cn.laowen.org.service.impl;

import cn.laowen.basic.exception.BusinessException;
import cn.laowen.basic.util.BaiduAuditUtils;
import cn.laowen.basic.vo.ShopVo;
import cn.laowen.org.domain.Employee;
import cn.laowen.org.domain.Shop;
import cn.laowen.org.domain.ShopAuditLog;
import cn.laowen.org.mapper.EmployeeMapper;
import cn.laowen.org.mapper.ShopAuditLogMapper;
import cn.laowen.org.mapper.ShopMapper;
import cn.laowen.org.service.IEmployeeService;
import cn.laowen.org.service.IShopService;
import cn.laowen.basic.service.impl.BasicServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laowen
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BasicServiceImpl<Shop> implements IShopService {
    //1.一般业务实现可以通公共业务实现层来实现

    //2.特殊的业务实现需要自己重写
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private IEmployeeService employeeServiceIpml;

    /**
     *审核通过
     * @param shopAuditLog
     */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //1.修改店铺状态为---审核通过
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(1);
        shopMapper.update(shop);
        //2.记录审核通过日志
        shopAuditLog.setShopId(shop.getId());//设置日志店铺id
        shopAuditLog.setAuditId(shop.getAdminId());//设置日志管理员id
        shopAuditLog.setAuditTime(new Date());//设置日志时间为当前时间
        shopAuditLog.setState(1);//设置日志状态为1通过
        shopAuditLogMapper.save(shopAuditLog);
        //3.发送激活邮件
        Employee employee = employeeMapper.loadByShopId(shopAuditLog.getShopId());
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1377636906@qq.com");
        //收件人
        mailMessage.setTo(employee.getEmail());
        //邮件主题
        mailMessage.setSubject("宠物乐园-店铺入驻激活连接:");
        //邮件内容
        mailMessage.setText("亲爱的用户,您的店铺审核已通过,请在两个工作日值内完成激活并使用,激活链接:"
                +"http://localhost:8080/shop/activation/"+shopAuditLog.getShopId());
        javaMailSender.send(mailMessage);


    }

    /**
     * 不同店铺的审核状态数量
     * @return
     */
    @Override
    public List<ShopVo> queryEcharts() {
        return shopMapper.loadEcharts();
    }

    /**
     * 批量新增
     * @param shops
     */
    @Override
    public void bulkSave(List<Shop> shops) {
        shopMapper.bulkSave(shops);
    }

    /**
     * 驳回
     * @param shopAuditLog
     */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //1.修改店铺状态为---驳回
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        //2.记录审核通过日志
        shopAuditLog.setShopId(shop.getId());//设置日志店铺id
        shopAuditLog.setAuditId(shop.getAdminId());//设置日志管理员id
        shopAuditLog.setAuditTime(new Date());//设置日志时间为当前时间
        shopAuditLog.setState(-1);//设置日志状态为-1驳回
        shopAuditLogMapper.save(shopAuditLog);
        //3.发送激活邮件
        Employee employee = employeeMapper.loadByShopId(shopAuditLog.getShopId());
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1377636906@qq.com");
        //收件人
        mailMessage.setTo(employee.getEmail());
        //邮件主题
        mailMessage.setSubject("宠物乐园-店铺入驻驳回任务:");
        //邮件内容
        mailMessage.setText("亲爱的用户,您的信息填写有误,审核不通过,请尽快修改并重新提交审核!!!修改链接:"
                +"http://localhost:8081/#/register2?shopId="+shopAuditLog.getShopId());
        javaMailSender.send(mailMessage);
    }

    /**
     * 店铺入驻
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        //一.校验参数是否为空:
        Employee admin = shop.getAdmin();//店长
        if (StringUtils.isEmpty(shop.getName())//店铺名或者店铺地址或者店铺管理员账号为空,抛出异常
                ||StringUtils.isEmpty(shop.getAddress())||StringUtils.isEmpty(admin.getUsername())
        ){
            throw new BusinessException("参数不能为空!");
        }
        //二.合法校验(利用百度AI智能审核接口)
        if (!BaiduAuditUtils.TextCensor(shop.getName())){//如果店铺名非法
            throw new BusinessException("小伙子不要搞事哦,你这个店铺名是人能想的出来的(?.?)");
        }
        //三.判断是否已经注册
            //1.店铺不能重复注册
        Shop shopTmp = shopMapper.loadByName(shop.getName());//根据店铺名查询
        if(shopTmp !=null){//如果店铺已存在
            throw new BusinessException("店铺已被注册,请直接登录!");
        }
            //2.店铺管理人是否已经注册
        Employee employeeTmp = employeeMapper.loadByUserName(admin.getUsername());//根据店铺管理员账号查询
                //2.1注册了并且已经绑定店铺了
        if (employeeTmp !=null && employeeTmp.getShop_id() !=null){
            throw new BusinessException("该用户已绑定店铺,请更换一名用户!");
        }
                //2.2注册了但是没有绑定店铺
        if(employeeTmp ==null){//
            employeeServiceIpml.save(admin);//xml中要开启自增Id,返回自增Id
            employeeTmp = admin;
        }
        //四.保存数据
        shop.setAdminId(employeeTmp.getId());//给店铺绑定管理员
        shopMapper.save(shop);//xml中要开启自增Id,返回自增Id
        employeeTmp.setShop_id(shop.getId());//给管理员添加店铺id
        employeeMapper.update(employeeTmp);


    }
}
